/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VizConfigFrame.java
 *
 * Created on 08.06.2009, 17:07:08
 */
package peripheral.viz;

import java.io.File;
import java.util.logging.Level;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import peripheral.logic.Logging;
import processing.core.PApplet;

/**
 *
 * @author Berni
 */
public class VizStartupFrame extends javax.swing.JFrame {

    JFileChooser configChooser;
    java.io.File configFile;

    /** Creates new form VizConfigFrame */
    public VizStartupFrame() {
        initComponents();
        Logging.getLogger().setLevel(Level.INFO);
        this.setSize(550, 400);
        this.setLocation(
                getToolkit().getScreenSize().width / 2 - this.getWidth() / 2,
                getToolkit().getScreenSize().height / 2 - this.getHeight() / 2 - 50);
        this.setResizable(false);

        configChooser = new JFileChooser();
        configChooser.setFileFilter(new ZipFilter());
    //this.logoLabel.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/res/pdc.png"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        fileLabel = new javax.swing.JLabel();
        selectFileButton = new javax.swing.JButton();
        runButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Peripheral - Visualization");

        fileLabel.setText("<select a file>");

        selectFileButton.setText("Choose...");
        selectFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectFileButtonActionPerformed(evt);
            }
        });

        runButton.setText("Run");
        runButton.setEnabled(false);
        runButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(27, 27, 27)
                .add(fileLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 182, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(154, 154, 154)
                .add(selectFileButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(runButton)
                .add(35, 35, 35))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(selectFileButton)
                    .add(runButton)
                    .add(fileLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/peripheral/resources/PDCVis300x300.png"))); // NOI18N
        getContentPane().add(jLabel1, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void selectFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectFileButtonActionPerformed

        if (configChooser.showOpenDialog(null) == configChooser.APPROVE_OPTION) {
            configFile = configChooser.getSelectedFile();
            fileLabel.setText(configFile.getName());
            this.runButton.setEnabled(true);
        }
    }//GEN-LAST:event_selectFileButtonActionPerformed

    private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runButtonActionPerformed

        if (configFile != null) {

            this.setVisible(false);
            this.dispose();
            PApplet.main(new String[]{"--present", "peripheral.viz.VisApplet", configFile.getAbsolutePath()});
        }
    }//GEN-LAST:event_runButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        System.out.println("working dir=" + (new File(".")).getAbsolutePath());
        System.out.println("java.library.path=" + System.getProperty("java.library.path"));
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new VizStartupFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel fileLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton runButton;
    private javax.swing.JButton selectFileButton;
    // End of variables declaration//GEN-END:variables

    private class ZipFilter extends FileFilter {

        //Accept all directories and all gif, jpg, tiff, or png files.
        public boolean accept(java.io.File f) {
            if (f.isDirectory()) {
                return true;
            }

            if (f.getName().toLowerCase().endsWith(".zip")) {
                return true;
            }

            return false;
        }

        //The description of this filter
        public String getDescription() {
            return "ConfigurationFiles";
        }
    }
}
